﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyRead.API.Appicant.Command;
using B.S.DataBase.MyRead.API.Appicant.Command.RoleMenus;
using B.S.DataBase.MyRead.API.DTOs;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace B.S.DataBase.MyRead.API.Appicant.CommandHandler.RoleMenus
{
    public class GetRoleMenuCommendHandlers : IRequestHandler<GetRoleMenuCommends, APIResult<List<RoleMenu>>>
    {

        private readonly IBaseRepository<Role> roleRepository;
        private readonly IBaseRepository<Menu> menuRepository;
        private readonly IBaseRepository<RoleMenu> rolemenuRepository;
        private readonly IMapper mapper;

        public GetRoleMenuCommendHandlers(IBaseRepository<Role> roleRepository, IBaseRepository<Menu> menuRepository, IBaseRepository<RoleMenu> rolemenuRepository, IMapper mapper)
        {
            this.roleRepository = roleRepository;
            this.menuRepository = menuRepository;
            this.rolemenuRepository = rolemenuRepository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 根据角色获取菜单信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<APIResult<List<RoleMenu>>> Handle(GetRoleMenuCommends request, CancellationToken cancellationToken)
        {
            APIResult<List<RoleMenu>> result = new APIResult<List<RoleMenu>>();
       
            var list = rolemenuRepository.GetAll().Where(x => x.RoleId == request.RoleId).ToList();
       
   
            result.Code = APIEnum.查询成功;
            result.Msg = APIEnum.查询成功.ToString();
            result.data = list.ToList();

            return Task.FromResult(result);
        }
    }
}
